/**
 * 
 */
package com.ctn.information.data.retriever.impl.sample;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.ctn.information.data.model.Menu;
import com.ctn.information.data.model.MenuLevel;
import com.ctn.information.data.retriever.Interface.MenuRetriever;


/**
 * Sample Implementation for test only
 * @author Zale
 *
 */
public class MenuRetrieverSampleImpl implements MenuRetriever{
	private List<Menu> menus;
	
	
	public MenuRetrieverSampleImpl() {
		super();
		initDefaults();
	}

	private void initDefaults() {
		if(menus == null){
			menus = new ArrayList<Menu>();
		}
		menus.add(createMenu(1L,"新闻",MenuLevel.ONE));
		menus.add(createMenu(2L,"企业",MenuLevel.ONE));
		menus.add(createMenu(3L,"经销商",MenuLevel.ONE));
		menus.add(createMenu(4L,"行情",MenuLevel.ONE));
		menus.add(createMenu(5L,"微博",MenuLevel.ONE));
		menus.add(createMenu(6L,"手机报",MenuLevel.ONE));
		menus.add(createMenu(7L,"专题",MenuLevel.ONE));
		menus.add(createMenu(8L,"大讲堂",MenuLevel.ONE));
		menus.add(createMenu(9L,"采购团",MenuLevel.ONE));
		menus.add(createMenu(10L,"采购节",MenuLevel.ONE));
		menus.add(createMenu(11L,"渠道之旅",MenuLevel.ONE));
		
		menus.add(createMenu(12L,"宏观",MenuLevel.TWO));
		menus.add(createMenu(13L,"LED",MenuLevel.TWO));
		menus.add(createMenu(14L,"灯饰",MenuLevel.TWO));
		menus.add(createMenu(15L,"电工",MenuLevel.TWO));
		menus.add(createMenu(16L,"人物",MenuLevel.TWO));
		menus.add(createMenu(17L,"专栏",MenuLevel.TWO));
	}

	private Menu createMenu(Long id ,String label, MenuLevel level) {
		Menu menu = new Menu();
		menu.setLabel(label);
		menu.setLevel(level);
		menu.setId(id);
		return menu;
	}

	@Override
	public Menu getById(Long id) {
		for(Menu menu:menus){
			if(menu.getId() == id){
				return menu;
			}
		}
		return null;
	}

	@Override
	public List<Menu> getAll(boolean asc) {
		return menus;
	}

	@Override
	public List<Menu> getMenuByLevel(MenuLevel level,Long id) {
		List<Menu> levelMenu = new ArrayList<Menu>();
		for(Menu menu:menus){
			if(menu.getLevel().equals(level)){
				 levelMenu.add(menu);
			}
		}
		Collections.sort(levelMenu);
		return levelMenu;
	}

	@Override
	public List<Menu> getSubMenuFromCache(MenuLevel level) {
		return getMenuByLevel(level,null);
	}

}
